package org.apache.flink.runtime.rest.handler.job.metrics;

import java.util.Map;

import javax.annotation.Nullable;

import org.apache.flink.api.common.time.Time;
import org.apache.flink.runtime.clusterframework.types.ResourceID;
import org.apache.flink.runtime.rest.handler.HandlerRequest;
import org.apache.flink.runtime.rest.legacy.metrics.MetricFetcher;
import org.apache.flink.runtime.rest.legacy.metrics.MetricStore;
import org.apache.flink.runtime.rest.messages.EmptyRequestBody;
import org.apache.flink.runtime.rest.messages.job.metrics.TaskManagerMetricsHeaders;
import org.apache.flink.runtime.rest.messages.job.metrics.TaskManagerMetricsMessageParameters;
import org.apache.flink.runtime.rest.messages.taskmanager.ResourceProviderIdPathParameter;
import org.apache.flink.runtime.rpc.retriever.GatewayRetriever;
import org.apache.flink.runtime.webmonitor.RestfulGateway;

/**
 * Handler that returns Provider Manager metrics.
 *
 * @see MetricStore#getTaskManagerMetricStore(String)
 */
public class ProviderManagerMetricsHandler
        extends AbstractMetricsHandler<TaskManagerMetricsMessageParameters> {

    public ProviderManagerMetricsHandler(
            final GatewayRetriever<? extends RestfulGateway> leaderRetriever,
            final Time timeout,
            final Map<String, String> headers,
            final MetricFetcher metricFetcher) {
        super(
                leaderRetriever,
                timeout,
                headers,
                TaskManagerMetricsHeaders.getInstance(),
                metricFetcher);
    }

    @Nullable
    @Override
    protected MetricStore.ComponentMetricStore getComponentMetricStore(
            final HandlerRequest<EmptyRequestBody> request, final MetricStore metricStore) {
        final ResourceID taskManagerId = request.getPathParameter(ResourceProviderIdPathParameter.class);
        return metricStore.getTaskManagerMetricStore(taskManagerId.toString());
    }
}
